package game.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.dbcp2.BasicDataSource;



public class DatabaseUtil {
	private BasicDataSource dataSource;
	private static DatabaseUtil instance = new DatabaseUtil();
	
	private DatabaseUtil() {
		dataSource = new BasicDataSource();
		dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
		dataSource.setUrl("jdbc:oracle:thin:@127.0.0.1:1521:XE");
		dataSource.setUsername("scott");
		dataSource.setPassword("tiger");
		
		dataSource.setInitialSize(2);
		dataSource.setMaxTotal(10);
	}
	
	public static DatabaseUtil getInstance(){
		return instance;
	}
	
	public Connection getConnection() throws SQLException{
		return dataSource.getConnection();
	}
	
	public void close(Connection conn, Statement stmt) throws SQLException{
		if(stmt != null)	stmt.close();
		if(conn != null) conn.close();
	}
	
	public void close(Connection conn, Statement stmt, ResultSet rs) throws SQLException{
		if(rs != null) rs.close();
		if(stmt != null)	stmt.close();
		if(conn != null) conn.close();
	}
}